package com.shibaobei.activity.controller;

import com.alibaba.fastjson.JSONObject;
import com.shibaobei.activity.model.*;
import com.shibaobei.activity.service.ApplicationService;
import com.shibaobei.activity.service.ApproveInfoService;
import com.shibaobei.activity.service.FormTemplateService;
import com.shibaobei.activity.service.FormflowService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/4/29 0029.
 */
@Controller
@RequestMapping("/formflow")
public class FormflowController {
    @Autowired
    private FormTemplateService templateService;
    @Autowired
    private FormflowService  formflowService;
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private ApproveInfoService approveInfoService;

    @RequestMapping("/formTemplateList")
    public String formTemplateList(Model model){
        List<Template> list = templateService.findList();
        model.addAttribute("templateList",list);
        return "Flow_Formflow/formTemplateList";
    }

    @RequestMapping("/submitUI")
    public String submitUI(String templateId,Model model){
        model.addAttribute("templateId",templateId);
        return "Flow_Formflow/submitUI";
    }

    /**
     *提交申请
     * @param templateId
     * @param resource
     * @return
     */
    @RequestMapping("/submit")
    public ModelAndView submit(String templateId, MultipartFile resource,HttpSession session){
        formflowService.submit(templateId,resource,session);

        ModelAndView mv = new ModelAndView();
        //使用重定向，此时springmvc.xml配置文件中的视图解析器将会失效
        mv.setViewName("redirect:/formflow/formTemplateList");
        return mv;
    }


    /**
     *待我审批
     * @return
     */
    @RequestMapping("/myTaskList")
    public String myTaskList(Model model,HttpSession session){
        List<TaskView> list = formflowService.findMyTaskList((User)session.getAttribute("user"));
        model.addAttribute("myTaskList",list);
        return "Flow_Formflow/myTaskList";
    }

    /**
     * 流转记录
     * @param applicationId
     * @param model
     * @return
     */
    @RequestMapping("/approvedHistory")
    public String approvedHistory(String applicationId,Model model){
        List<ApproveInfo> list = approveInfoService.findListByApplicationId(applicationId);
        model.addAttribute("list",list);
        return "Flow_FormFlow/approvedHistory";
    }

    /**
     * 审批
     * @param applicationId
     * @param taskId
     * @param model
     * @return
     */
    @RequestMapping("/approveUI")
    public String approveUI(String applicationId,String taskId,Model model){
        model.addAttribute("applicationId",applicationId);
        model.addAttribute("taskId",taskId);
        return  "Flow_FormFlow/approveUI";
    }

    @RequestMapping("/approve")
    public ModelAndView approve(Integer applicationId,String taskId,
                          Boolean approval,String comment,HttpSession session){
        User user = (User) session.getAttribute("user");
        approveInfoService.approve(applicationId,taskId,approval,comment,user);

        ModelAndView mv = new ModelAndView();
        //使用重定向，此时springmvc.xml配置文件中的视图解析器将会失效
        mv.setViewName("redirect:/formflow/myTaskList");
        return mv;
    }

    /**
     * 查询当前登录人的提交申请
     * @param model
     * @return
     */
    @RequestMapping("/mySubmittedList")
    public String mySubmittedList(Model model,String status,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Application> list = applicationService.findByApplicant(user.getId(),status);
        model.addAttribute("list",list);
        return "Flow_FormFlow_Old/mySubmittedList";
    }
    @RequestMapping("/showPng")
    @ResponseBody
    public JSONObject showPng(Integer applicationId){
        //根据流程变量查询当前任务
        Task task = formflowService.findTaskByApplicationId(applicationId);
        //根据当前的任务查询对应的流程定义对象
        ProcessDefinition pd = formflowService.findPdByTask(task);

        JSONObject json = new JSONObject();
        json.put("deploymentId",pd.getDeploymentId());
        json.put("imageName",pd.getDiagramResourceName());
        //根据任务查询坐标
        Map<String,Object> map = formflowService.findCoordingByTask(task);

        json.put("acs",map);
        json.put("y","70");
        json.put("width","55");
        json.put("height","105");
        return json;
    }
    @RequestMapping("/viewImage")
    @ResponseBody
    public byte[] viewImage(String deploymentId,String imageName) throws IOException {
        InputStream inputStream = formflowService.showPng(deploymentId,imageName);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }



}
